<?php

namespace App\Http\Controllers\Api;

use App\Api\Services\StorageManageService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class StorageManageController extends BaseController
{
    /**
     * @库存列表
     * created 在 2021/11/03 14:44 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
    */
    public function list(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return $this->success(
            StorageManageService::list($this->getMainUserId(), $data)
        );
    }

    /**
     * @库存列表
     * created 在 2021/11/03 14:58 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
    */
   public function export(Request $request)
   {
       $data = $request->all();
       StorageManageService::export($this->getMainUserId(), $data);
   }

   /**
    * @入库
    * @author SunWu
    * @param Request $request
    * @return \Illuminate\Http\JsonResponse
    */
   public function inStorage()
   {
//       $data['content'] = '[{"name":"\u725b\u536b\u58eb\u6d17\u8863\u51dd\u73e0","type_name":"\u9999\u6c34","warehouse":"B1-9\u680b","warehouse_id":48,"num":200}]';
       $data['content'] = [
           [
               'name' => '20升冷藏箱',
               'type_name' => '00001',
               'warehouse' => '仓库1',
               'warehouse_id' => 1158,
               'num' => 10
           ],
//           [
//               'name' => '时尚烟酰胺亮颜保湿面膜',
//               'type_name' => '保湿',
//               'warehouse' => '蚌埠掌上网络科技有限公司',
//               'warehouse_id' => 3,
//               'num' => 190
//           ]
       ];

       $data['all_id_type'] = 1;
       $data['all_id'] = 10;
       $data['out_storage_time'] = '2021-11-13 13:14:15';
       $data['in_storage_time'] = '2021-11-13 13:14:15';
       $data['remark'] = '这是出库';

//       return StorageManageService::cancelout(1, 10);

       return response()->json(
//           GeTuiService::push()
//        StorageManageService::inStorage(
           StorageManageService::outStorage(
               $this->getUserId(),
               $this->getMainUserId(),
               $data
          )
       );

   }

    /**
     * @直接入库
     * @created 在 2021/11/30 11:30 创建
     * @param Request $request
     * @author: Sun Wu
     */
   public function directInStorage(Request $request)
   {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'materiel_name' => 'required',//物料名称
            'type_name' => 'required',//物料型号
            'storage_id' => 'required|numeric',//仓库id
            'storage_name' => 'required',//仓库名
            'status' => [
            'required',
            Rule::In([0, 1]),
            ],//是否设置低库存预警数：0-否 1-是
//            'in_storage_time' => 'required|date_format:Y-m-d H:i:s',//入库时间
            'num' => 'required|numeric|min:0',//入库数量
            'alert_num' => 'numeric|min:0'
        ]);

       //参数错误响应
       if($info = $validator->errors()->first()) {
           return response()->json([
               'code' => 1,
               'msg' => $info,
               'data' => '',
            ]);
       }

       $data['user_id'] = $this->getUserId();
       $data['main_id'] = $this->getMainUserId();
       $data['batch_title'] = $this->batchTitle();
       $this->log('新建直接入库', $data);
       return response()->json(
           StorageManageService::directInStorage($data)
       );
   }

    /**
     * @设置低库存预警数
     * @created 在 2021/11/30 16:54 创建
     * @param Request $request
     * @author: Sun Wu
     */
   public function setAlertNum(Request $request)
   {
       //参数验证
       $validator = Validator::make($data = $request->all(), [
           'id' => 'required',//库存物料id
           'alert_num' => 'required|numeric|min:0'
       ]);

       //参数错误响应
       if($info = $validator->errors()->first()) {
           return response()->json([
               'code' => 1,
               'msg' => $info,
               'data' => '',
           ]);
       }

       $data['main_id'] = $this->getMainUserId();
       $this->log('设置低库存预警数', $data);
       return response()->json(
           StorageManageService::setAlertNum($data)
       );
   }

    /**
     * @删除
     * @created 在 2021/11/30 17:26 创建
     * @param Request $request
     * @author: Sun Wu
     */
   public function del(Request $request)
   {
       //参数验证
       $validator = Validator::make($data = $request->all(), [
           'id' => 'required',//库存物料id
       ]);

       //参数错误响应
       if($info = $validator->errors()->first()) {
           return response()->json([
               'code' => 1,
               'msg' => $info,
               'data' => '',
           ]);
       }

       $data['main_id'] = $this->getMainUserId();
       $this->log('删除库存信息', $data);
       return response()->json(
           StorageManageService::del($data)
       );
   }

    /**
     * @调拨
     * @created 在 2021/12/2 11:20 创建
     * @param Request $request
     * @author: Sun Wu
     */
   public function allocate(Request $request)
   {
       //参数验证
       $validator = Validator::make($data = $request->all(), [
           'id' => 'required|integer',//库存物料id
           'storage_id' => 'required|integer',//调出目标仓库id
           'allocate_num' => 'required|numeric|min:0',//调出目标仓库id
       ],
           [
               'id.required' => '参数`id`缺失',
               'id.integer' => '参数`id`只能为数字',
               'allocate_num.required' => '参数`allocate_num`缺失',
               'allocate_num.integer' => '参数`allocate_num`只能为数字',
               'allocate_num.min' => '参数`allocate_num`最小只能为0',
       ]);
       //参数错误响应
       if($info = $validator->errors()->first()) {
           return response()->json([
               'code' => 1,
               'msg' => $info,
               'data' => '',
           ]);
       }

       $data['user_id'] = $this->getUserId();
       $data['main_id'] = $this->getMainUserId();
       $data['batch_title'] = $this->batchTitle();

       $this->log('调拨', $data);
       return response()->json(
           StorageManageService::allocate($data)
       );
   }

    /**
     * @直接出库
     * @created 在 2021/12/2 14:58 创建
     * @param Request $request
     * @author: Sun Wu
     */
   public function directOutStorage(Request $request)
   {
       //参数验证
       $validator = Validator::make($data = $request->all(), [
           'id' => 'required|integer',//库存物料id
           'num' => 'required|numeric|min:0',//出库数量
       ]);

       //参数错误响应
       if($info = $validator->errors()->first()) {
           return response()->json([
               'code' => 1,
               'msg' => $info,
               'data' => '',
           ]);
       }

       $data['user_id'] = $this->getUserId();
       $data['main_id'] = $this->getMainUserId();

       $this->log('直接出库', $data);
       return response()->json(
           StorageManageService::directOutStorage($data)
       );
   }
}
